﻿@using System.Globalization;
@using Microsoft.AspNetCore.Builder;
@using Microsoft.Extensions.Options;
@inherits Fluxor.Blazor.Web.Components.FluxorLayout
@inject ContextMenuService contextMenuService
@inject IDispatcher dispatcher
@inject DialogService dialogService
@inject NavigationManager Navigation
@inject IOptions<RequestLocalizationOptions> requestLocalizationOptions

<PageTitle>@Global.WebsiteTitle</PageTitle>

<RadzenDialog />
<RadzenNotification />
<RadzenContextMenu />
<RadzenTooltip />

<RadzenLayout class="rz-shadow-3">
    <RadzenHeader>
        <div class="d-flex align-items-center">
            <div class="p-2">
                <RadzenSidebarToggle Click="@(() => sidebarExpanded = !sidebarExpanded)" />
            </div>
            <div class="p-2">
                <RadzenImage Path="_content/SimpleIdServer.CredentialIssuer.Website/images/SIDLogo.svg" Style="width: 40px" />
            </div>
            <div class="p-2">
                <RadzenText Style="margin: 0; color: white;" Text="SimpleIdServer" TextStyle="TextStyle.DisplayH6" />
            </div>
            <div class="ml-auto"></div>
            <div class="p-2">
                <RadzenDropDown Class="w-100"
                                Data=@SupportedCultures
                                TValue="CultureInfo"
                                @bind-Value=@CurrentLanguage
                                TextProperty="DisplayName" />
            </div>
            <div class="p-2">
                <RadzenLink Text="@Global.SignOut" Icon="logout" Path="/logout" />
            </div>
        </div>
    </RadzenHeader>
    <RadzenSidebar @bind-Expanded="@sidebarExpanded">
        <RadzenPanelMenu>
            <RadzenPanelMenuItem Text="@Global.Overview" Icon="home" Path="/overview"></RadzenPanelMenuItem>
            <RadzenPanelMenuItem Text="@Global.CerdentialConfigurations" Icon="wallet" Path="/credentialconfs" />
            <RadzenPanelMenuItem Text="@Global.DeferredCredentialsTitle" Icon="wallet" Path="/deferredcreds" />
        </RadzenPanelMenu>
    </RadzenSidebar>
    <RadzenBody>
        <div class="p-2">
            @Body
        </div>
    </RadzenBody>
</RadzenLayout>

@code {
    bool sidebarExpanded = true;

    IList<CultureInfo> SupportedCultures => requestLocalizationOptions.Value.SupportedCultures;

    CultureInfo CurrentLanguage
    {
        get => CultureInfo.CurrentCulture;
        set
        {
            if (CultureInfo.CurrentCulture != value)
            {
                var uri = new Uri(Navigation.Uri)
                    .GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
                var cultureEscaped = Uri.EscapeDataString(value.ToString());
                var uriEscaped = Uri.EscapeDataString(uri);
                Navigation.NavigateTo(
                    $"Culture/Set?culture={cultureEscaped}&redirectUri={uriEscaped}",
                    forceLoad: true);
            }
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
    }
}